﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.EntityClient;
using System.Linq;
using System.Text;
using AnimalCare2012.Model;

namespace AnimalCare2012
{
    public class ProductionBLL
    {
        testDBEntities db;

        public ProductionBLL(IDbConnection con)
        {
            //db = new testDBEntities();
            db = new testDBEntities((EntityConnection)con);
        }

        public ProductionBLL()
        {
            db = new testDBEntities();
        }

        public ProductionBLL(string connectionString)
        {
            //db = new testDBEntities(connectionString);
        }

        public List<production> Find(string name)
        {
            List<production> list = null;

            var obj = db.productions.Where(p => p.pro_name.Contains(name));
            
            list = obj.ToList<production>();

            return list;
        }

        public int Insert(string pro_code, string pro_name, string ma_ncc, string ten_ncc)
        {
            production pro = new production();

            //pro = db.productions.Find(1);
            pro.id = 1; 
            pro.pro_code = pro_code;
            pro.pro_name = pro_name;

            NHACUNGCAP ncc = new NHACUNGCAP();
            ncc.TenNhaCungCap = ten_ncc;
            ncc.MaNhaCungCap = ma_ncc;

            //NHACUNGCAP ncc2;
            //ncc2 = db.NHACUNGCAPs.Find(ma_ncc);

            //pro.NHACUNGCAP = ncc;
            //pro.NHACUNGCAP = ncc2;
            //pro.NHACUNGCAP = null;
            //pro.manhacungcap = ncc.MaNhaCungCap;

            //pro.NHACUNGCAP.TenNhaCungCap = ten_ncc;

            //db.NHACUNGCAPs.Add(ncc);
            //db.productions.Add(pro);
            
            var TScope = new TransactionScope

            db.productions.Attach(pro);
            db.Entry(pro).State = EntityState.Modified;

            try
            {
                int i = db.SaveChanges();
                return i;
            }
            catch (Exception ex)
            {
                ex = ex.InnerException;
            }

            return 0;
        }

        public production FindByKey(int id)
        {
            production pro = null;

            pro = db.productions.Find(id);

            return pro;
        }

        public int Update(string pro_code, string pro_name)
        {
            //List<production> list = null;
            //try
            //{
            //    list = db.productions.Where(p => p.pro_code == pro_code).ToList();
            //}
            //catch (Exception ex)
            //{
            //}

            //if (list.Count == 0)
            //{
            //    return -1;
            //}

            //production product = list[0];

            production product = null;

            //try
            //{
            //    product = (from p in db.productions
            //               where p.pro_code.Equals(pro_code)
            //               select p).FirstOrDefault<production>();
            //}
            //catch (Exception ex)
            //{
            //    Exception iex = ex.InnerException;
            //}

            product = new production();

            product.id = 2;
            product.pro_code = pro_code;

            product.pro_name = pro_name;
            try
            {
                db.productions.Attach(product);
                db.Entry(product).State = EntityState.Modified;

                int i = db.SaveChanges();
                return i;
            }

            catch (Exception ex)
            {
                Exception iex = ex.InnerException;
                return -1;
            }
        }
    }
}